<template>
  <view class="container" style="background-color: #fff;">
	  <uni-forms ref="bmiForm" :rules="rulesBMI" :model="bmiData" labelWidth="140px">
			<uni-forms-item label="身高（cm）：" name="height">
				<uni-easyinput v-model="bmiData.height" placeholder="请输入身高" />
			</uni-forms-item>
			<uni-forms-item label="体重（kg）：" name="weight">
				<uni-easyinput v-model="bmiData.weight" placeholder="请输入体重" />
			</uni-forms-item>
			<uni-forms-item label="性别" name="gender">
				<uni-data-checkbox v-model="bmiData.gender" :localdata="sexs" />
			</uni-forms-item>
			<uni-forms-item label="年龄" name="age">
				<uni-data-select v-model="bmiData.age" :localdata="ageRanges" >
				</uni-data-select>
			</uni-forms-item>
	  </uni-forms>
      <button @click="submitBMI">计算BMI</button>
	 
	<view v-if="bmi">
		<view style="padding-top: 35rpx;">
		  <text>BMI值：</text>
		  <text>{{ bmi }}</text>
		</view>
		<view style="padding-top: 15rpx;">
		  <text>体重状态：</text>
		  <text>{{ weightStatus }}</text>
		</view>
		<view style="padding-top: 15rpx;">
		  <text>健康建议：</text>
		  <text>{{ healthAdvice }}</text>
		</view>
	</view>
  </view>
</template>

<script>
export default {
  data() {
    return {
	  ad:null,
	  ageRanges: [{value:0,text:'不满18岁'}, {value:1,text:'18-30岁'}, {value:2,text:'31-50岁'}, {value:3,text:'50岁以上'}],
      ageRange: ['不满18岁', '18-30岁', '31-50岁', '50岁以上'],
	  sexs:[{text: '男',value:'male'}, {text: '女',value: 'female'}],
      bmi: '',
      weightStatus: '',
      healthAdvice: '',
	  bmiData:{
		  gender: 'female'
	  },
	  rulesBMI: {
	  	gender: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '请选择性别'
	  		}]
	  	},
		age: {
			rules: [{
				required: true,
				errorMessage: '请选择年龄'
			}]
		},
		height: {
			rules: [{
				required: true,
				errorMessage: '请输入身高'
			}, {
				format: 'number',
				errorMessage: '身高只能输入数字'
			}]
		},
	  	weight: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '请输入体重'
	  		}, {
	  			format: 'number',
	  			errorMessage: '体重只能输入数字'
	  		}]
	  	}
	  }
    };
  },
  onLoad(option)
  {
	  
  },
  methods: {
	submitBMI()
	{
		this.$refs.bmiForm.validate().then(res => {
			 this.bmi = '';
			 this.weightStatus = '';
			 this.healthAdvice = '';
			 this.calculateBMI();
		}).catch(err => {
			console.log('err', err);
		})	
	},
    calculateBMI() {
      const heightInMeter = this.bmiData.height / 100;
      const bmiValue = this.bmiData.weight / (heightInMeter * heightInMeter);
      this.bmi = bmiValue.toFixed(2);

      // 根据BMI值和性别年龄判断体重状态和健康建议
      if (this.bmiData.gender === 'male') {
        // 男性的分类标准
        if (bmiValue < 20.7) {
          this.weightStatus = '体重过轻';
          this.healthAdvice = '建议增加饮食摄入量，适量进行锻炼增加肌肉';
        } else if (bmiValue < 26.4) {
          this.weightStatus = '体重正常';
          this.healthAdvice = '保持良好的饮食和运动习惯，注意平衡营养摄入';
        } else if (bmiValue < 27.8) {
          this.weightStatus = '超重';
          this.healthAdvice = '控制饮食，减少高热量食物的摄入，增加运动量';
        } else {
          this.weightStatus = '肥胖';
          this.healthAdvice = '改善饮食结构，合理安排运动时间和强度';
        }
      } else {
        // 女性的分类标准
        if (bmiValue < 19.1) {
          this.weightStatus = '体重过轻';
          this.healthAdvice = '建议增加饮食摄入量，适量进行锻炼增加肌肉';
        } else if (bmiValue < 25.8) {
          this.weightStatus = '体重正常';
          this.healthAdvice = '保持良好的饮食和运动习惯，注意平衡营养摄入';
        } else if (bmiValue < 27.3) {
          this.weightStatus = '超重';
          this.healthAdvice = '控制饮食，减少高热量食物的摄入，增加运动量';
        } else {
          this.weightStatus = '肥胖';
          this.healthAdvice = '改善饮食结构，合理安排运动时间和强度';
        }
      }
    },
    onAgeChange(event) {
      this.age = event.mp.detail.value;
    }
  }
};
</script>

<style>
.container {
  padding: 20px;
}
</style>
